package com.sailfish.springbootdemo.service.db2.impl;

import com.sailfish.springbootdemo.configs.UserHeaderHolder;
import com.sailfish.springbootdemo.dao.db2.PcbPermissionConfigDao;
import com.sailfish.springbootdemo.dao.db4.UserDao;
import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db2.PcbPermissionConfig;
import com.sailfish.springbootdemo.pojo.db4.User;
import com.sailfish.springbootdemo.service.db2.PcbPermissionConfigService;
import com.sailfish.springbootdemo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;
import java.util.List;

@Service
public class PcbPermissionConfigServiceImpl implements PcbPermissionConfigService {

    @Autowired
    PcbPermissionConfigDao permissionConfigDao;
    @Autowired
    UserDao userDao;

    @Override
    public List<PcbPermissionConfig> getPermissionConfigList() {
        List<PcbPermissionConfig> byLimit = permissionConfigDao.getByLimit(null);
        return byLimit;
    }

    @Transactional(transactionManager = "MysqlTransactionManager2")
    @Override
    public Result update(Integer id, Integer approver, String remark) {
        try {
            if (id == null) {
                return ResultUtil.error(500, "参数id为空", "param id is null", null);
            }
            if (approver == null) {
                return ResultUtil.error(500, "参数approver为空", "param approver is null", null);
            }
            String userId = UserHeaderHolder.getUserId();
            PcbPermissionConfig pcbPermissionConfig = new PcbPermissionConfig();
            pcbPermissionConfig.setApproveUserId(approver);
            User userInfoById = userDao.getUserInfoById(approver);
            pcbPermissionConfig.setApproveUserName(userInfoById.getUserName());
            pcbPermissionConfig.setRemark(remark);
            pcbPermissionConfig.setId(id);
            pcbPermissionConfig.setUpdateUser(userId);
            pcbPermissionConfig.setUpdateTime(new Date());
            permissionConfigDao.update(pcbPermissionConfig);
            return ResultUtil.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }

    }

}
